package com.zhang.zhangapp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.bean.user.RechargeItem;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2018/4/30.
 */

public class RechargeAdapter extends BaseAdapter {
    private List<RechargeItem> rechargeItemList;
    private Context context;
    private LinearLayout selectedView;
    private RechargeItem selectItem;

    public RechargeAdapter(List<RechargeItem> rechargeItemList, Context context) {
        this.rechargeItemList = rechargeItemList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return rechargeItemList == null ? 0 : rechargeItemList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_charge, null);
            x.view().inject(vh, view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if (i == 0) {
            vh.rechargeTagLayout.setVisibility(View.VISIBLE);
            setSelectedView(vh.rechargeLayout);
            selectItem = rechargeItemList.get(i);
        } else {
            vh.rechargeTagLayout.setVisibility(View.GONE);
        }
        vh.rechargeAmountText.setText(rechargeItemList.get(i).getAmountdesc());
        vh.rechargeDescText.setText(rechargeItemList.get(i).getLargessdesc());
        vh.rechargeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int j = 0; j < rechargeItemList.size(); j++) {
                    rechargeItemList.get(j).setSelected(false);
                }
                rechargeItemList.get(i).setSelected(true);
                LinearLayout layout = (LinearLayout) view;
                setSelectedView(layout);
                selectItem = rechargeItemList.get(i);
            }
        });
        return view;
    }

    private void setSelectedView(LinearLayout layout) {
        for (int j = 0; j < layout.getChildCount(); j++) {
            if (selectedView != null) {
                selectedView.getChildAt(j).setSelected(false);
            }
            layout.getChildAt(j).setSelected(true);
        }
        if (selectedView != null)
            selectedView.setSelected(false);
        layout.setSelected(true);
        selectedView = layout;
    }

    public RechargeItem getSelectedItem() {
        return selectItem;
    }

    class ViewHolder {
        @ViewInject(R.id.item_recharge)
        LinearLayout rechargeLayout;
        @ViewInject(R.id.item_recharge_amount)
        TextView rechargeAmountText;
        @ViewInject(R.id.item_recharge_amount_desc)
        TextView rechargeDescText;
        @ViewInject(R.id.item_charge_tag_layout)
        RelativeLayout rechargeTagLayout;
    }
}
